﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockPrjTeam2.Biz;
using MockPrjTeam2.Entity;
using MockPrjTeam2.DAO;

namespace MockPrjTeam2.Pages.Views
{
    public partial class TrustRegionList : System.Web.UI.Page
    {
        TrustRegionBiz trustRegionBiz = new TrustRegionBiz();
        private static int _flag;
        private static int _check;
        private static string _key = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _flag = 0;
                _check = 0;
                LoadAllTrustRegion();
            }
        }
        private void LoadAllTrustRegion()
        {
            grvTrustRegionList.DataSource = trustRegionBiz.GetAll();
            grvTrustRegionList.DataBind();
        }


        protected void StartWithByNames(object sender, CommandEventArgs e)
        {
            if (CheckBoxInActive.Checked)
            {
                _check = 1;
                _flag = 1;
                _key = e.CommandName;
                grvTrustRegionList.DataSource = trustRegionBiz.StartWithByNames(_key, _flag);
                grvTrustRegionList.DataBind();
            }
            else
            {
                _check = 1;
                _flag = 0;
                _key = e.CommandName;
                grvTrustRegionList.DataSource = trustRegionBiz.StartWithByNames(_key, _flag);
                grvTrustRegionList.DataBind();
            }
        }

        protected void LinkButtonTrustRegionName_Command(object sender, CommandEventArgs e)
        {
            Response.Redirect(string.Format(CultureInfo.CurrentCulture, "../Forms/TrustRegionDetails.aspx?id={0}", e.CommandName));
        }

        protected void CheckBoxInactiveCheckedChanged(object sender, EventArgs e)
        {
            if (_check == 1)
            {
                if (CheckBoxInActive.Checked && _check == 1)
                {
                    _flag = 1;
                    grvTrustRegionList.DataSource = trustRegionBiz.StartWithByNames(_key, _flag);
                    grvTrustRegionList.DataBind();
                }
                else if (CheckBoxInActive.Checked == false && _check == 1)
                {
                    _flag = 0;
                    grvTrustRegionList.DataSource = trustRegionBiz.StartWithByNames(_key, _flag);
                    grvTrustRegionList.DataBind();

                }

            }
            else if (_check == 0)
            {
                if (CheckBoxInActive.Checked && _check == 0)
                {
                    grvTrustRegionList.DataSource = trustRegionBiz.GetInActive();
                    grvTrustRegionList.DataBind();
                }
                else
                {
                    _flag = 0;
                    LoadAllTrustRegion();
                }
            }
        }

        protected void LbtnTrustRegionName_Command(object sender, CommandEventArgs e)
        {

            if (e.CommandArgument.Equals("False"))
            {
                int trustRegionId = int.Parse(e.CommandName, CultureInfo.CurrentCulture);
                bool result = trustRegionBiz.SetActive(trustRegionId, true);
            }

            Response.Redirect(string.Format(CultureInfo.CurrentCulture, "../Forms/TrustRegionDetails.aspx?id={0}", e.CommandName));
        }

        protected void grvTrustRegionList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvTrustRegionList.PageIndex = e.NewPageIndex;
            if (_check == 1)
            {
                if (CheckBoxInActive.Checked && _check == 1)
                {
                    _flag = 1;
                    grvTrustRegionList.DataSource = trustRegionBiz.StartWithByNames(_key, _flag);
                }
                else if (CheckBoxInActive.Checked == false && _check == 1)
                {
                    _flag = 0;
                    grvTrustRegionList.DataSource = trustRegionBiz.StartWithByNames(_key, _flag);

                }
            }
            else if (_check == 0)
            {
                if (CheckBoxInActive.Checked && _check == 0)
                {
                    grvTrustRegionList.DataSource = trustRegionBiz.GetInActive();
                }
                else
                {
                    _flag = 0;
                    grvTrustRegionList.DataSource = trustRegionBiz.GetAll();
                }
            }
            grvTrustRegionList.DataBind();
        }

        protected void grvTrustRegionList_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
                e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
            }
        }

        protected void btnCreat_Click(object sender, EventArgs e)
        {
            Response.Redirect("../Forms/AddTrustRegion.aspx");
        }
    }
}